<?php

class ProjectController extends MController
{
	/**
	 * 项目列表
	 */
	public function actionProjectList()
	{
		$request = array();
		// 额外请求字段 subtype 表示项目状态
		$params = array('itemtype', 'pageno', 'pagesize', 'userid', 'itemname', 'isshopping', 'myid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		
		$itemid = false;
		if (!empty($request['userid'])) {
			$itemid = $this->model('ItemUser')->getItemIdByUserID($request['userid']);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$list = $this->model('item')->getList($request['pageno'], $request['pagesize'], $request['itemtype'], $itemid, 
			'itemid,supportmoney suportmoney,remark,title,titlelogo,createtime,starttime,endtime,howmuch,fhowmuch,qihao,author,size,itemtype', $request['itemname'], (int) $request['isshopping']);
		$result['code'] = 0;
		if (!empty($list)) {
			foreach ($list as $item) {
				$item['remark'] = $this->util->sub_string(strip_tags(trim($item['remark']), '<br>'), 0, 50);
				$item['createtime'] = date('Y-m-d H:i:s', $item['createtime']);
				$item['logopath'] = !empty($item['titlelogo']) ? ITEM_LOGO_URL . '/' . $item['titlelogo'] : '';
				$item['itemtype'] = Yii::app()->params['item_type'][$item['itemtype']];
				$item['state'] = $this->_formatDate($item['starttime']);
				$item['endtime'] = date('Y-m-d', $item['endtime']);
				$item['replynum'] = $this->model('reply')->getCountByItemID($item['itemid'], 2);
				if (!empty($request['myid']) && $this->model('Guanzhu')->isFans($request['myid']) !== false) {
					$item['howmuch'] = $item['fhowmuch'];
				}
				unset($item['titlelogo']);
				if ((int) $request['isshopping'] == 1) {
					$state = $this->model('Consume')->getStateByItemId($item['itemid']);
				} else {
					$state = $this->model('Reply')->getStateByItemId($item['itemid']);
				}
				if (!$state) {
					//$state = array('suportcount' => '0', 'suportmoney' => '0');
					$state = array('suportcount' => '0');
				}
				$item = array_merge($item, $state);
				$result['projects'][] = $item;
			}
		}
		echo json_encode($result);die;
	}
	
	public function actionDelitem()
	{
		$res = array('code' => 1, 'msg' => '失败');
		$itemid = $this->getRequest('itemid');
		if ($this->model('Item')->deleteByPk($itemid) !== false) {
			$res = array('code' => 0, 'msg' => '成功');
		}
		echo json_encode($res);die;
	}
	
	public function actionProjectdeltaillist()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid', 'itemtype', 'priority');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		if (!in_array($request['priority'], array(1,2,3))) {
			$request['priority'] = 1;
		}
		
		$itemid = false;
		if (!empty($request['userid'])) {
			$itemid = $this->model('ItemUser')->getItemIdByUserID($request['userid']);
		}
		$result['code'] = 0;
		//echo json_encode($result);die;
		if (!$itemid) {
			$result['projects'] = array();
			echo json_encode($result);die;
		}
		
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$list = $this->model('item')->getList1($request['pageno'], $request['pagesize'], $request['itemtype'], $itemid,
				'itemid,supportmoney,title,remark,titlelogo,createtime,itemtype', '', (int) $request['priority']);
		if (!empty($list)) {
			foreach ($list as $item) {
				$item['replynum'] = $this->model('reply')->getCountByItemID($item['itemid'], 2);
				$item['remark'] = strip_tags($item['remark'], '<br>');
				$itemuser = $this->model('ItemUser')->getByItemID($item['itemid']);
				$user = $this->model('user')->getUserById($itemuser['userid'], 'userid authorid,logoid,nickname as author');
				if ($user['logoid'] > 0) {
					$photo = $this->model('photo')->findByPk($user['logoid']);
					$user['authorlogo'] = PHOTO_URL . '/' . $photo['largepath'];
					unset($user['logoid']);
				}
				$item['createtime'] = date('Y-m-d H:i:s', $item['createtime']);
				$item['logopath'] = !empty($item['titlelogo']) ? ITEM_LOGO_URL . '/' . $item['titlelogo'] : '';
				$item['photos'] = '';
				$photoid = $this->model('ItemPhoto')->getListByItemID($item['itemid'], 'GROUP_CONCAT(photoid) photoid');
				if (!empty($photoid)) {
					$photoid = $photoid[0]['photoid'];
					$photo = $this->model('photo')->getListIn($photoid);
					if (!empty($photo)) {
						foreach ($photo as $p) {
							$item['photos'][] = array(
								'picurl' => PHOTO_URL . '/' . $p['largepath']
							);
						}
					}
				}
				unset($item['titlelogo']);
				if (!$user) {
					$user = array('author' => '', 'authorid' => '0', 'authorlogo' => '');
				}
				$item = array_merge($item, $user);
				$result['projects'][] = $item;
			}
		}
		echo json_encode($result);die;
	}
	
	/**
	 * 项目详情
	 */
	public function actionProjectInfo()
	{
		$itemid = $this->getRequest('itemid');
		$userid = $this->getRequest('userid');
		$myid = $this->getRequest('myid');
		if (!$itemid) {
			$this->return_json(1, 'itemid不能为空');
		}
		$item = $this->model('item')->findByPk($itemid);
		if (!$item) {
			$this->return_json(1, '项目不存在');
		}
		$this->model('Item')->addVisit($item['itemid']);
		$item['createtime'] = date('m-d H:i', $item['createtime']);
		$item['starttime'] = date('Y-m-d', $item['starttime']);
		$item['endtime'] = date('Y-m-d', $item['endtime']);
		$item['logopath'] = ITEM_LOGO_URL . '/' . $item['titlelogo'];
		$item['replynum'] = $this->model('reply')->getCountByItemID($item['itemid'], 2);
		$item['surportnum'] = $this->model('reply')->getCountByItemID($item['itemid'], 1);
		$item['howmuch'] = (int) $item['howmuch'];
		$item['remark'] = strip_tags($item['remark'], '<br>');
		$item['supportback'] = strip_tags($item['supportback'], '<br>');
		if (!empty($myid) && $this->model('Guanzhu')->isFans($myid) !== false) {
			$item['howmuch'] = $item['fhowmuch'];
		}
		unset($item['titlelogo']);
		$result['projectinfo'] = $item;
		//临时增加 待确定
		$result['projectinfo']['hasmoney'] = '0';
		$ispay = !$userid ? '0' : $this->model('consume')->isExist($userid, $itemid);
		$result['projectinfo']['ispay'] = !$ispay ? '0' : $ispay;
		$reluser = array();
		$item_user = $this->model('ItemUser')->getListByItemID($itemid);
		if (!empty($item_user)) {
			foreach ($item_user as $i => $user) {
				$logo = $this->model('user')->getUserById($user['userid'], 'logoid,nickname as truename');
				$photo = $this->model('photo')->findByPk($logo['logoid']);
				$reluser[$i]['userid'] = $user['userid'];
				$reluser[$i]['name'] = $logo['truename'];
				$reluser[$i]['logopath'] = !empty($photo) ? PHOTO_URL . '/' . $photo['smallpath'] : '';
				//$reluser[$i]['howmuch'] = $user['howmuch'];
				//$reluser[$i]['howmuch'] = $this->model('gongxian')->getSumMuch($user['userid']);
				//$reluser[$i]['howmuch'] = $this->model('reply')->getSumMuch($user['userid'], $item['itemid']);
				$reluser[$i]['howmuch'] = $this->model('reply')->getSumMuchByUserId($user['userid'], $item['itemid']);
			}
		}
		$result['reluser'] = $reluser;
		
		echo json_encode($result);die;
	}
	
	public function actionProjectInfoPhoto()
	{
		$itemid = $this->getRequest('itemid');
		if (!$itemid) {
			$this->return_json(1, 'itemid不能为空');
		}
		$photoid = $this->model('ItemPhoto')->getListByItemID($itemid, 'GROUP_CONCAT(photoid) photoid');
		$result = array(
			'code' => 0,
			'msg' => '成功',
			'viewphoto' => array(),
			'photos' => array()
		);
		if (!empty($photoid)) {
			$photoid = $photoid[0]['photoid'];
			$photo = $this->model('photo')->getListIn($photoid);
			if (!empty($photo)) {
				foreach ($photo as $item) {
					if ($item['isview'] == 1) {
						$result['viewphoto'][] = array(
							'picurl' => PHOTO_URL . '/' . $item['path']
						);
					} else {
						$result['photos'][] = array(
							'picurl' => PHOTO_URL . '/' . $item['path']
						);
					}
				}
			}
		}
		echo json_encode($result);die;
	}
	
	public function actionProjectInfoMovie()
	{
		$itemid = $this->getRequest('itemid');
		if (!$itemid) {
			$this->return_json(1, 'itemid不能为空');
		}
		$item_movie = $this->model('ItemMovie')->getListByItemID($itemid);
		$result = array(
			'code' => 0,
			'msg' => '成功',
			'movies' => array()
		);
		if (!empty($item_movie)) {
			foreach ($item_movie as $item) {
				$result['movies'][] = array(
					'picurlr' => MOVIE_URL . '/' . $item['moviepic'],
					'moviepath' => $item['moviepath']
				);
			}
		}
		echo json_encode($result);die;
	}
	
	public function actionProjectInfoReply()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'itemid', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$list = $this->model('reply')->getListByItemId($request['itemid'], $request['userid'], 2, $request['pageno'], $request['pagesize'], 'r.replyerid,r.userid,u.nickname as replyername,p.midpath replyerlogo, r.content,r.replytime createtime,r.picurl filepath');
		if (!empty($list)) {
			foreach ($list as $i => $item) {
				$list[$i]['replyerlogo'] = !empty($list[$i]['replyerlogo']) ? PHOTO_URL . '/' . $item['replyerlogo'] : '';
				$list[$i]['createtime'] = date('m-d H:i', $item['createtime']);
				$list[$i]['filepath'] = !empty($item['filepath']) ? PHOTO_URL . '/' . $item['filepath'] : '';
				$list[$i]['usertruename'] = $this->model('user')->getUserNickName($item['userid']);
				$list[$i]['content'] = strip_tags($item['content'], '<br>');
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['replys'] = $list;
		echo json_encode($result);die;
	}
	
	/**
	 * 我的购买
	 */
	public function actionMybuy()
	{
		$request = array();
		$params = array('itemtype', 'pageno', 'pagesize', 'userid', 'isshopping');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		$itemid = false;
		if (!empty($request['userid'])) {
			$consume = $this->model('consume')->getItemIdByUserID($request['userid'], 'itemid,createtime');
			if (!empty($consume)) {
				foreach ($consume as $buy) {
					if ($buy['itemid'] == '0') continue;
					$itemid .= ',' . $buy['itemid'];
					$buytime[$buy['itemid']] = date('Y-m-d H:i:s', $buy['createtime']);
				}
				$itemid = ltrim($itemid, ',');
			}
		}
		$result['code'] = 0;
		if ($itemid == false) {
			echo json_encode($result);die;
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$list = $this->model('item')->getMyList($request['pageno'], $request['pagesize'], $request['itemtype'], $itemid,
				'itemid,supportmoney suportmoney,remark,title,titlelogo,createtime,starttime,endtime,howmuch,fhowmuch,replynum,qihao,author,size,itemtype', '', (int) $request['isshopping']);
		if (!empty($list)) {
			foreach ($list as $item) {
				$item['remark'] = $this->util->sub_string(strip_tags(trim($item['remark']), '<br>'), 0, 50);
				$item['createtime'] = date('Y-m-d H:i:s', $item['createtime']);
				$item['logopath'] = ITEM_LOGO_URL . '/' . $item['titlelogo'];
				$item['itemtype'] = Yii::app()->params['item_type'][$item['itemtype']];
				$item['buytime'] = $buytime[$item['itemid']];
				if (!empty($request['userid']) && $this->model('Guanzhu')->isFans($request['userid']) !== false) {
					$item['howmuch'] = $item['fhowmuch'];
				}
				if ((int) $request['isshopping'] == 1) {
					$state = $this->model('Consume')->getStateByItemId($item['itemid']);
				} else {
					$state = $this->model('Reply')->getStateByItemId($item['itemid']);
				}
				if (!$state) {
					//$state = array('suportcount' => '0', 'suportmoney' => '0');
					$state = array('suportcount' => '0');
				}
				$item = array_merge($item, $state);
				unset($item['titlelogo']);
				$result['projects'][] = $item;
			}
		}
		echo json_encode($result);die;
	}
	
	/**
	 * 评论接口
	 */
	public function actionAddReply()
	{
		$request = array();
		$params = array('replyerid', 'content', 'itemid', 'userid', 'replytype');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		$data = array(
			'replyerid' => $request['replyerid'],
			'content' => $request['content'],
			'itemid' => (int) $request['itemid'],
			'userid' => (int) $request['userid'],
			'replytype' => $request['replytype'],
			'replytime' => time(),
		);
		
		// 暂时停止保存图片
		// if ($file = CUploadedFile::getInstanceByName('file')) {
		// 	if (!empty($file)) {
		// 		if (!in_array($file->getExtensionName(), explode(',', IMG_UPLOAD_TYPES))) {
		// 			$this->return_json(1, '图片格式不正确');
		// 		}
		// 		if ($file->getSize() > IMG_UPLOAD_SIZE) {
		// 			$this->return_json(1, '图片超出大小');
		// 		}
		// 		$file_name = md5(uniqid()) . '.' . $file->getExtensionName();
		// 		if ($file->saveAs(PHOTO_DIR . '/' . $file_name)) {
		// 			$data['picurl'] = $file_name;
		// 		}
		// 	}
		// }

		if ($this->model('reply')->insert($data) !== false) {
			if ($data['userid'] > 0) {
				$this->model('reply')->updateNewsNum($request['userid']);
			}
			$this->return_json(0, '发布成功');
		}
		$this->return_json(1, '发布失败');
	}
	
	/**
	 * 分享接口
	 */
	public function actionAddshare()
	{
		$request = array();
		$params = array('jfcode', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		$data = array(
			'jfcode' => $request['jfcode'],
			'userid' => $request['userid'],
			'createtime' => time()
		);
		if ($this->model('jifen')->insert($data) !== false) {
			$this->return_json(0, '分享成功');
		}
		$this->return_json(1, '分享失败');
	}
	
	public function actionJoin()
	{
		$request = array();
		$params = array('userid', 'itemid');
		foreach ($params as $item) {
			$request[$item] = (int) $this->getRequest($item);
		}
		
		if ($request['userid'] < 1 || $request['itemid'] < 1) {
			$this->return_json(1, '参数不正确');
		}
		
		if ($this->model('ItemUser')->save($request['userid'], $request['itemid']) !== false) {
			$this->return_json(0, '订阅成功');
		}
		$this->return_json(1, '订阅失败');
	}
	
	public function actionSupportta(){
		// TODO:fill logic about support in project to target girl
		$request = array();
		$params = array('userid', 'itemid','target','source');
		foreach ($params as $item) {
			$request[$item] = (int) $this->getRequest($item);
		}

		echo json_encode(array(
			'code' => 0,
			'msg' => '支持成功',
			'remain' => 0,
		));
		die;

		// $this->return_json(0, '支持成功');
	}

	private function _formatDate($time)
	{
		if ($time < time()) {
			return '未开始';
		} elseif ($time > time()) {
			return '已结束';
		} else {
			return '进行中';
		}
	}
}